define(["require", "exports", "./util"], function (require, exports, util_1) {
    "use strict";
    exports.__esModule = true;
    exports.buildElement = exports.parseUtil = void 0;
    exports.parseUtil = {
        bind: function (me, value, fun, shouldChange) {
            if (util_1.mve.isGValueOrDelaySet(value)) {
                var old_1;
                if (util_1.mve.isDelaySet(value)) {
                    me.WatchAfter(value, function (v) {
                        if (shouldChange(old_1, v)) {
                            old_1 = v;
                            value.after(v, fun);
                        }
                    });
                }
                else {
                    me.WatchAfter(value, function (v) {
                        if (shouldChange(old_1, v)) {
                            old_1 = v;
                            fun(v);
                        }
                    });
                }
            }
            else {
                fun(value);
            }
        },
        bindKV: function (me, map, fun, shouldChange) {
            mb.Object.forEach(map, function (v, k) {
                exports.parseUtil.bind(me, v, function (value) {
                    fun(k, value);
                }, shouldChange);
            });
        }
    };
    /**原始的组装*/
    function buildElement(fun) {
        var out = function (n) {
            return function (parent, me) {
                var out = fun(me, n);
                parent.push(out.element);
                return out.init;
            };
        };
        out.one = fun;
        out.root = function (cal) {
            var life = (0, util_1.newLifeModel)();
            var result = fun(life.me, cal(life.me));
            var init = result.init;
            result.init = (0, util_1.onceLife)(function () {
                var destroy = (0, util_1.orRun)(init);
                return function () {
                    life.destroy();
                    (0, util_1.orRun)(destroy);
                };
            }).init;
            return result;
        };
        return out;
    }
    exports.buildElement = buildElement;
});
